﻿using System.Collections.Generic;
using System.Text;
using DreamStore.WebShop.Extensibility;
using DreamStore.WebShop.Models;
using Orchard.Environment.Extensions;
using Orchard.Messaging.Services;
using Orchard.Users.Models;

namespace DreamStore.WebShop.Services
{
    [OrchardFeature("DreamStore.WebShop.EmailSenderSP")]
    public class EmailSenderServiceProvider : IEmailSenderServiceProvider
    {
        private readonly IMessageManager _messageManager;

        public EmailSenderServiceProvider(IMessageManager messageManager)
        {
            _messageManager = messageManager;
        }

        public void SendSignupMail(UserPart user, CustomerPart customer)
        {
            const string subject = "Your registration at The Dream Store";

            var bodyBuilder = new StringBuilder();
            bodyBuilder.AppendLine("<h3>Welcome to the Dream Store</h3><br/>");
            bodyBuilder.AppendLine(string.Format("Hello {0} {1}<br/>", customer.FirstName, customer.LastName));
            bodyBuilder.AppendLine("<br/>");
            bodyBuilder.AppendLine("You have succesfully registered as a customer at The Dream Store<br/>");
            bodyBuilder.AppendLine("We're looking forward to doing business with you - <a href='http://www.youtube.com/watch?v=mhN93rFZuJs' target='_blank'>It's business time!</a><br/>");
            bodyBuilder.AppendLine("<br/>");
            bodyBuilder.AppendLine(string.Format("You username is: {0}<br/>", user.UserName));
            bodyBuilder.AppendLine("<br/>");
            bodyBuilder.AppendLine("Regards The Dream Store Team<br/>");
            bodyBuilder.AppendLine("<br/>");
            bodyBuilder.AppendLine("http://91.144.247.230");

            var mailData = new Dictionary<string, string>
                           {
                               {"Subject", subject},
                               {"Body", bodyBuilder.ToString()}
                           };
            _messageManager.Send(user.ContentItem.Record, "SignupConfirmationEmail", "email", mailData);
        }

        public void SendOrderConfirmationMail(OrderRecord order, IEnumerable<OrderLine> orderLines, UserPart user, CustomerPart customer)
        {
            const string subject = "Order confirmation - The Dream Store";

            var bodyBuilder = new StringBuilder();
            bodyBuilder.AppendLine(string.Format("Hello {0} {1},<br/>", customer.FirstName, customer.LastName));
            bodyBuilder.AppendLine("<br/>");
            bodyBuilder.AppendLine("Thank you for your order<br/>");
            bodyBuilder.AppendLine("<br/>");
            bodyBuilder.AppendLine("Order details:<br/>");
            bodyBuilder.AppendLine("<br/>");
            bodyBuilder.AppendFormat("Order number: {0}<br/>", order.Number);
            bodyBuilder.AppendLine("<br/>");
            bodyBuilder.AppendLine("<table>");
            bodyBuilder.AppendLine("<thead><tr><td>Product Name</td><td>Quantity</td><td>Unit Price</td><td>Total</td></tr></thead>");
            bodyBuilder.AppendLine("<tbody>");
            foreach (var orderLine in orderLines)
            {
                bodyBuilder.AppendLine(string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>", orderLine.ProductName, orderLine.Quantity, orderLine.UnitPrice.ToString("c"), orderLine.TotalPrice.ToString("c")));
            }
            bodyBuilder.AppendLine("<tr><td colspan='4'>&nbsp;</td></tr>");
            bodyBuilder.AppendFormat("<tr><td colspan='3'>Total:</td><td>{0}</td></tr>", order.Total.ToString("c"));
            bodyBuilder.AppendLine("</tbody>");
            bodyBuilder.AppendLine("</table>");
            bodyBuilder.AppendLine("<br/>");
            bodyBuilder.AppendLine("Regards The Dream Store Team<br/>");
            bodyBuilder.AppendLine("<br/>");
            bodyBuilder.AppendLine("http://91.144.247.230");

            var mailData = new Dictionary<string, string>
                           {
                               {"Subject", subject},
                               {"Body", bodyBuilder.ToString()}
                           };
            _messageManager.Send(user.ContentItem.Record, "OrderConfirmationEmail", "email", mailData);
        }
    }
}